<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-left">
        <div class="welcome-content">
          <h2>欢迎使用</h2>
          <h1>EasyTool 系统</h1>
          <p class="description">专业的数据分析工具，为您提供精准的赛事分析和数据支持</p>
          <div class="features">
            <div class="feature-item">
              <el-icon class="feature-icon"><DataAnalysis /></el-icon>
              <span>精准数据分析</span>
            </div>
            <div class="feature-item">
              <el-icon class="feature-icon"><TrendCharts /></el-icon>
              <span>实时数据更新</span>
            </div>
            <div class="feature-item">
              <el-icon class="feature-icon"><Monitor /></el-icon>
              <span>直观可视化</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="login-right">
        <el-card class="login-card" shadow="never">
          <div class="card-header">
              <h2>系统登录</h2>
          </div>

          <el-form ref="loginFormRef" :model="loginForm" :rules="rules" @submit.prevent="handleLogin" size="large">
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                placeholder="请输入用户名" 
                clearable 
                size="large"
                class="login-input"
              >
                <template #prefix>
                  <el-icon class="input-icon"><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input 
                type="password" 
                v-model="loginForm.password" 
                placeholder="请输入密码" 
                show-password
                size="large"
                class="login-input"
              >
                <template #prefix>
                  <el-icon class="input-icon"><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item>
              <el-button 
                type="primary" 
                @click="handleLogin" 
                :loading="loading" 
                size="large" 
                class="login-button"
              >
                {{ loading ? '登录中...' : '登录' }}
              </el-button>
            </el-form-item>
          </el-form>
          
          <div class="login-footer">
            <p>© 2025 EasyTool 系统. 保留所有权利.</p>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import http from '../utils/http'
import { useRouter } from 'vue-router'
import { DataAnalysis, TrendCharts, Monitor, User, Lock } from '@element-plus/icons-vue'

const loginFormRef = ref<FormInstance>()
const loading = ref(false)
const router = useRouter()

const loginForm = reactive({
  username: '',
  password: ''
})

const rules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const response = await http.post('/login', loginForm)
        loading.value = false

        if (response.status === 200) {
          ElMessage.success('登录成功！')
          localStorage.setItem('loggedIn', 'true')
          const expirationTime = new Date().getTime() + 3600 * 1000
          localStorage.setItem('loginExpiration', expirationTime.toString())
          router.push({ path: '/home' })
        } else {
          ElMessage.error(response.data.message || '登录失败')
        }
      } catch (error) {
        loading.value = false
        ElMessage.error('登录请求失败，请稍后重试')
      }
    } else {
      ElMessage.error('请检查输入信息')
    }
  })
}
</script>

<style scoped>
/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #1a5fa5 0%, #3191d2 100%);
  padding: 20px;
  overflow: hidden;
}

.login-wrapper {
  display: flex;
  width: 100%;
  max-width: 900px;
  height: 500px;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  max-height: calc(100vh - 40px);
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, #2980b9 0%, #3498db 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: white;
  position: relative;
}

.welcome-content h2 {
  font-size: 24px;
  font-weight: 300;
  margin-bottom: 10px;
  opacity: 0.9;
}

.welcome-content h1 {
  font-size: 36px;
  font-weight: 600;
  margin-bottom: 20px;
}

.description {
  font-size: 16px;
  line-height: 1.6;
  margin-bottom: 30px;
  opacity: 0.9;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
}

.feature-icon {
  font-size: 20px;
}

.login-right {
  flex: 1;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  /* padding: 10px; */
}

.login-card {
  width: 100%;
  border: none;
  border-radius: 12px;
  background-color: white;
}

.card-header {
  text-align: center;
  padding: 30px 0;
}

.card-header h2 {
  margin: 0 0 10px 0;
  color: #3395d6;
  font-size: 36px;
}

.subtitle {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

:deep(.el-card__header) {
  border-bottom: 1px solid #ebeef5;
  padding: 0 30px;
}

:deep(.el-card__body) {
  padding: 30px;
}

.login-input {
  height: 50px;
}

/* 输入框图标样式 */
.input-icon {
  color: #909399;
  font-size: 18px;
}

:deep(.el-input__prefix) {
  left: 12px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  padding-left: 42px; /* 为图标留出空间 */
}

:deep(.el-input__wrapper:focus-within) {
  box-shadow: 0 0 0 2px rgba(41, 128, 185, 0.2);
  border-color: #2980b9;
}

.login-button {
  width: 100%;
  height: 50px;
  margin-top: 10px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  background-color: #2980b9;
  border-color: #2980b9;
}

.login-button:hover {
  background-color: #1a5fa5;
  border-color: #1a5fa5;
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.login-footer p {
  margin: 0;
  color: #909399;
  font-size: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-wrapper {
    flex-direction: column;
    height: auto;
    min-height: auto;
    max-height: calc(100vh - 40px);
    overflow-y: auto;
  }
  
  .login-left {
    padding: 30px 20px;
    min-height: 220px;
  }
  
  .welcome-content h1 {
    font-size: 28px;
  }
}

@media (max-width: 480px) {
  .login-container {
    padding: 10px;
  }
  
  .login-wrapper {
    max-height: calc(100vh - 20px);
  }
}
</style>